package com.dp.mallchat.common.user.dao;

import com.dp.mallchat.common.user.domain.user.entity.UserRole;
import com.dp.mallchat.common.user.mapper.UserRoleMapper;
import com.dp.mallchat.common.user.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色关系表 服务实现类
 * </p>
 *
 * @author <a href="https://gitee.com/baiDP">adu</a>
 * @since 2024-06-13
 */
@Service
public class UserRoleDao extends ServiceImpl<UserRoleMapper, UserRole> {



    public List<UserRole> listByUid(Long uid) {
        List<UserRole> list = lambdaQuery()
                .eq(UserRole::getUid, uid)
                .list();
        return list;
    }

    @Cacheable(cacheNames = "user", key = "'userRole_'+#uid")
    public Set<Long> listRoleId(Long uid){
        List<UserRole> userRoles = listByUid(uid);
        if (CollectionUtils.isEmpty(userRoles)) {
            return null;
        }

        return userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
    }
}
